package lianxi03;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2024-04-21
 * Time: 13:23
 */
public class MyThread extends Thread{
    //同时开启两个线程,共同获取1-100之间的所有数字
    static int num = 1;
    static Object obj = new Object();

    @Override
    public void run() {
        while(true) {
            synchronized (obj) {
                if (num <= 100) {
                    try {
                        Thread.sleep(10);

                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    if(num%2 == 1) {
                        System.out.println(getName()+"获取的奇数是:"+num);
                    }
                    num++;
                }else {
                    break;
                }

            }
        }
    }
}
